package com.liuya.common;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.Assert;

/**
 * @Author 刘亚
 * @Date 2016/12/15
 */
public class CookieUtils {

    public static String cookiePath = "/";
    public static String cookieDomain = "";

    public static void addCookie(HttpServletRequest request,
                                 HttpServletResponse response, String name, String value,
                                 Integer maxAge, String path, String domain, Boolean secure) {
        Assert.notNull(request);
        Assert.notNull(response);
        Assert.hasText(name);
        try {
            name = URLEncoder.encode(name, "UTF-8");
            value = URLEncoder.encode(value, "UTF-8");
            Cookie localCookie = new Cookie(name, value);
            if (maxAge != null)
                localCookie.setMaxAge(maxAge.intValue());
            if (StringUtils.isNotEmpty(path))
                localCookie.setPath(path);
            if (StringUtils.isNotEmpty(domain))
                localCookie.setDomain(domain);
            if (secure != null)
                localCookie.setSecure(secure.booleanValue());
            response.addCookie(localCookie);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    public static void addCookie(HttpServletRequest request,
                                 HttpServletResponse response, String name, String value, Integer maxAge) {
        addCookie(request, response, name, value, maxAge, cookiePath, cookieDomain, null);
    }

    /**
     * 添加cookie信息
     * @param request
     * @param response
     * @param name
     * @param value
     */
    public static void addCookie(HttpServletRequest request,
                                 HttpServletResponse response, String name, String value) {
        addCookie(request, response, name, value, null, cookiePath, cookieDomain, null);
    }

    public static String getCookie(HttpServletRequest request, String name) {
        Assert.notNull(request);
        Assert.hasText(name);
        Cookie[] cookies = request.getCookies();
        if (cookies != null)
            try {
                name = URLEncoder.encode(name, "UTF-8");
                for (Cookie cookie : cookies)
                    if (name.equals(cookie.getName()))
                        return URLDecoder.decode(cookie.getValue(), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        return null;
    }

    /**
     * 删除cookie
     * @param request
     * @param response
     * @param name
     * @param path
     * @param domain
     */
    public static void removeCookie(HttpServletRequest request,
                                    HttpServletResponse response, String name, String path, String domain) {
        Assert.notNull(request);
        Assert.notNull(response);
        Assert.hasText(name);
        try {
            name = URLEncoder.encode(name, "UTF-8");
            Cookie cookie = new Cookie(name, null);
            cookie.setMaxAge(0);
            if (StringUtils.isNotEmpty(path))
                cookie.setPath(path);
            if (StringUtils.isNotEmpty(domain))
                cookie.setDomain(domain);
            response.addCookie(cookie);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除cookie
     * @param request
     * @param response
     * @param name
     */
    public static void removeCookie(HttpServletRequest request, HttpServletResponse response, String name) {
        removeCookie(request, response, name, cookiePath, cookieDomain);
    }
}